/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.find;

import controllers.*;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import constants.Constants;
import javax.swing.JFrame;
import view.book.CheckOutDlg;
import view.book.GeneralInf;
public class Customer extends javax.swing.JDialog {

    
    public Customer(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        init();
        tbCustomer.setModel(modelTable);
    }
   private void init(){
        MainController = new MainController();
        modelTable=new model.CustomerModel(CustomerController.findName(""));
   }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCustomer = new javax.swing.JTable();
        cbFind = new javax.swing.JComboBox();
        btCheckin = new javax.swing.JButton();
        btCheckout = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btDel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btView = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        tbBill = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tìm kiếm khách hàng");

        tbCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbCustomer);

        cbFind.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tìm kiếm theo tên", "Tìm kiếm theo phòng", "Tìm kiếm theo CMND", "Tìm kiếm theo tình trạng khách", "Tìm kiếm hóa đơn" }));
        cbFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFindActionPerformed(evt);
            }
        });

        btCheckin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/checkin.png"))); // NOI18N
        btCheckin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCheckinActionPerformed(evt);
            }
        });

        btCheckout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/checkout.png"))); // NOI18N
        btCheckout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCheckoutActionPerformed(evt);
            }
        });

        btEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit1.png"))); // NOI18N
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });

        btDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/del.png"))); // NOI18N
        btDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDelActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("TÌM KIẾM KHÁCH HÀNG");

        btView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/details.png"))); // NOI18N
        btView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btViewActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/find.png"))); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        tbBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/bil.png"))); // NOI18N
        tbBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbBillActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 775, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbFind, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(jButton5)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btView, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(btDel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(btEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(btCheckout, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(btCheckin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tbBill, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(228, 228, 228)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(82, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tfName, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cbFind, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(21, 21, 21)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(btCheckin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCheckout)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btDel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btView)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tbBill)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(52, 52, 52))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFindActionPerformed
       
        if(cbFind.getSelectedIndex()==0){
            System.out.println(cbFind.getSelectedIndex());
        }else{
                                
        }
    }//GEN-LAST:event_cbFindActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        name = tfName.getText().trim();
        switch(cbFind.getSelectedIndex()){
            case 0:
                findbyName();
                break;
            case 1:
                findbyRoom();
                break;
            case 2:
                findbyIdcard();
                break;
            case 3:
                findbyStatus();
                break;
            default:    
        }        
    }//GEN-LAST:event_jButton5ActionPerformed

    private void btCheckinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCheckinActionPerformed
      if(tbCustomer.getSelectedRowCount()>0){   
        this.getId();
            status =InfoBookController.getStatus(id);
            if(status==Constants.STATUS_BOOK){
                 new GeneralInf(new JFrame(),null, true,false,true, id).setVisible(true);
                 loadModelTable();
            }else{
                JOptionPane.showMessageDialog(this," Khách "+Constants.BOOK_NAME[status]+" ,không thể check in !");
            }
       }else  
            JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng check in");                
        
    }//GEN-LAST:event_btCheckinActionPerformed

    private void btCheckoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCheckoutActionPerformed
      if(tbCustomer.getSelectedRowCount()>0){        
            this.getId();
            status =InfoBookController.getStatus(id);
            if(status==Constants.STATUS_CHECKIN){
                 new CheckOutDlg(new JFrame(),null,true,id).setVisible(true);
                    loadModelTable();
                    this.dispose();
            }else{
                JOptionPane.showMessageDialog(this," Khách "+Constants.BOOK_NAME[status]+" ,không thể check out !");
            }
      }else  
            JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng check out");           
    }//GEN-LAST:event_btCheckoutActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
      if(tbCustomer.getSelectedRowCount()>0){
                getId();
                status =InfoBookController.getStatus(id);
                if(status!=Constants.STATUS_CHECKOUT&&status!=Constants.STATUS_FULL){
                    new GeneralInf(new JFrame(),null, true,status,true, id).setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(this," Khách "+Constants.BOOK_NAME[status]+" ,không thể sửa thông tin !");
                }            
        }else  
            JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng muốn sửa"); 
    }//GEN-LAST:event_btEditActionPerformed

    private void btViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViewActionPerformed
         
            if(tbCustomer.getSelectedRowCount()>0){
                getId();
                status =InfoBookController.getStatus(id);
                if(status!=Constants.STATUS_CHECKOUT){
                   new GeneralInf(new JFrame(),null, true, true, false,id).setVisible(true);
                }else{
                   new CheckOutDlg(new JFrame(),null,true,id).setVisible(true); 
                }                                            
            }else  
                    JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng muốn xem"); 
            
    }//GEN-LAST:event_btViewActionPerformed

    private void btDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDelActionPerformed
            if(tbCustomer.getSelectedRowCount()>0){
            int choice = JOptionPane.showConfirmDialog(this," Chắc chắn muốn xóa ?", "Xóa",JOptionPane.YES_NO_OPTION);
            if(choice== JOptionPane.YES_OPTION){
            getId();
            MainController.removeAll(id);
            ((model.CustomerModel) tbCustomer.getModel()).removeRow(realIndex);
            System.out.println("Xóa hàng trên giao diện");
            tbCustomer.repaint();                        
           }
           
       }else{
           JOptionPane.showMessageDialog(this,"Vui lòng chọn khách hàng cần xóa");
       }
    }//GEN-LAST:event_btDelActionPerformed

    private void tbBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbBillActionPerformed
        
    }//GEN-LAST:event_tbBillActionPerformed
    private void findbyName(){
            
                if(name.equals("")||CustomerController.findName(name)==null){
                JOptionPane.showMessageDialog(this,"không tìm thấy khách hàng");
                }else{
                    modelTable = new model.CustomerModel(CustomerController.findName(name));
                    loadModelTable();
                }

    }
    private void findbyRoom(){
         JOptionPane.showMessageDialog(this, "Tính năng này chưa hỗ trỡ");
        

    }
    private void findbyIdcard(){
                if(!Validate.isCMT(name)||CustomerController.findIdcard(new Integer(name)) ==null||!Validate.isCMT(name)){
                    JOptionPane.showMessageDialog(this,"Không tìm thấy khách hàng ");
                }else{
                    modelTable = new model.CustomerModel(CustomerController.findIdcard(new Integer(name)));
                    loadModelTable();
                }

    }
    private void findbyStatus(){        
         JOptionPane.showMessageDialog(this, "Tính năng này chưa hỗ trỡ");        

    }
   
    
    
    
    
    private void loadModelTable(){
                tbCustomer.setModel(modelTable);
                tbCustomer.repaint();
    }
  private void getId(){
        realIndex = tbCustomer.convertRowIndexToModel(tbCustomer.getSelectedRow());
        id = (int) ((model.CustomerModel) tbCustomer.getModel()).getValueAt(realIndex,1);  
  }
  class BookingListSelection implements ListSelectionListener {

        public BookingListSelection(){        
        }
        public void valueChanged(ListSelectionEvent e) {
            
                        if(!e.getValueIsAdjusting()){
                 }          
            }        
        }
  
    private String name;
    private int status;    
    private int id;
    private int realIndex;
    private AbstractTableModel modelTable;
    private MainController MainController;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCheckin;
    private javax.swing.JButton btCheckout;
    private javax.swing.JButton btDel;
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btView;
    private javax.swing.JComboBox cbFind;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton tbBill;
    private javax.swing.JTable tbCustomer;
    private javax.swing.JTextField tfName;
    // End of variables declaration//GEN-END:variables
}
